import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder

# 加载数据集
data = pd.read_csv('dataset_traffic_accident_prediction1.csv')

# 初始化LabelEncoder
le = LabelEncoder()

# 对所有分类特征进行独热编码
categorical_cols = ['Weather', 'Road_Type', 'Time_of_Day', 'Road_Condition', 'Vehicle_Type', 'Driver_Alcohol', 'Road_Light_Condition', 'Accident_Severity']
for col in categorical_cols:
    data[col] = le.fit_transform(data[col])

# 将目标变量转换为二进制
data['Accident'] = data['Accident'].apply(lambda x: 1 if x == 1 else 0)

# 计算相关系数矩阵
correlation_matrix = data.corr()

# 创建热力图
plt.figure(figsize=(12, 10))
sns.heatmap(correlation_matrix, annot=True, fmt=".2f", cmap='coolwarm', cbar=True)
plt.title('Correlation Heatmap of Features with Accident')
plt.xlabel('Features')
plt.ylabel('Features')
plt.show()




import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
import shap

# 1. 读取数据
file_path = "D:\\python vs\\dataset_traffic_accident_prediction1.csv"
data = pd.read_csv(file_path)

# 2. 数据清洗
# 填充数值型列的缺失值
data['Traffic_Density'] = data['Traffic_Density'].fillna(data['Traffic_Density'].median())

# 填充类别型列的缺失值
data['Accident_Severity'] = data['Accident_Severity'].fillna(data['Accident_Severity'].mode()[0])

# 3. 特征工程
# 独热编码
data = pd.get_dummies(data, columns=['Weather', 'Road_Type', 'Time_of_Day', 'Road_Condition', 'Vehicle_Type', 'Road_Light_Condition', 'Accident_Severity'], drop_first=True)

# 获取Weather相关的独热编码列
weather_columns = [col for col in data.columns if 'Weather_' in col]

# 创建交互特征
data['Weather_Alcohol_Interaction'] = data['Driver_Alcohol'] * data[weather_columns].sum(axis=1)

# 处理目标变量中的NaN值
data['Weather_Alcohol_Interaction'] = data['Weather_Alcohol_Interaction'].fillna(0)

# 4. 特征选择
X = data.drop(['Weather_Alcohol_Interaction'], axis=1)  # 特征
y = data['Weather_Alcohol_Interaction']  # 目标变量

# 5. 数据划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 6. 特征缩放
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# 7. 建模
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train_scaled, y_train)

# 8. 评估
y_pred = model.predict(X_test_scaled)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse}")
print(f"R²: {r2}")

# ----------------------------------------------------
# 9. 可视化：特征重要性图
feature_importances = model.feature_importances_
features = X.columns
feature_importance_df = pd.DataFrame({
    'Feature': features,
    'Importance': feature_importances
})
feature_importance_df = feature_importance_df.sort_values(by='Importance', ascending=False)

plt.figure(figsize=(10, 6))
sns.barplot(x='Importance', y='Feature', data=feature_importance_df)
plt.title("Feature Importance")
plt.show()

# ----------------------------------------------------
# 10. 可视化：实际值 vs 预测值对比图
plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred, color='blue', alpha=0.6)
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red', linestyle='--')
plt.title('Actual vs Predicted')
plt.xlabel('Actual Values')
plt.ylabel('Predicted Values')
plt.show()

# ----------------------------------------------------
# 11. SHAP 分析：解释模型决策逻辑
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test_scaled)

# 11.1. 绘制全局特征重要性图
shap.summary_plot(shap_values, X_test, plot_type="bar")

# 11.2. 绘制一个样本的SHAP值图
shap.initjs()  # 初始化JS
shap.force_plot(explainer.expected_value[1], shap_values[1][0], X_test.iloc[0])

